//
//  LPKHomeController.m
//  Demo-01
//
//  Created by 李平宽 on 2017/1/20.
//  Copyright © 2017年 com.lpk. All rights reserved.
//

#import "LPKHomeController.h"
#import "Masonry.h"
#import "LPKNetWorkTools.h"
#import "YYModel.h"
#import "LPKHomeMode.h"
#import "LPKHomeViewCell.h"
#import "LPKDetailsController.h"


static NSString *ID = @"cell";
@interface LPKHomeController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)LPKDetailsController *vc;

@end

@implementation LPKHomeController
{
    
    // 数据源数组
    NSArray *_dataList;
    NSArray *_dataDetailList;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self loadData];
   
    
}
- (void)setupUI
{
    
    
    [self.tableView registerClass:[LPKHomeViewCell class] forCellReuseIdentifier:ID];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.estimatedRowHeight = 400;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
}

#pragma mark - UITableViewDataSource


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LPKHomeViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    LPKHomeMode *model = _dataList[indexPath.row];
    
    cell.model = model;
    
   
    return cell;
}

#pragma mark - UITableViewDelegate

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LPKDetailsController *vc = [LPKDetailsController new];
    LPKHomeMode *pushId = _dataList[indexPath.row];
    vc.Id = pushId._id;
    self.hidesBottomBarWhenPushed  =YES;
    [self.navigationController pushViewController:vc animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}

#pragma mark - 请求数据

- (void)loadData
{
    [[LPKNetWorkTools sharedNetWorkTools] HomeViewData:nil success:^(id responseObject) {
    
        NSString *key = @"result";
        NSArray *dictArr = responseObject[key];
      
        _dataList = [NSArray yy_modelArrayWithClass:[LPKHomeMode class] json:dictArr];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
    
}

@end
